﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMS_Master.Dto;
using TMS_Master.Entities;
using TMS_Master.Interface;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace TMS_Master.DomainService
{
    public class Statistical_GoodsService: ApplicationService, IStatistical_GoodsService
    {
        #region 注入仓储接口
        private readonly IRepository<Statistical_Goods, Guid> _cargoRepository; //物资统计

        private readonly IRepository<TmsType, Guid> _typeRepository; //分类

        private readonly IRepository<Texture, Guid> _textureRepository; //材质

        private readonly IRepository<Address, Guid> _addrRepository;  //地址

        public Statistical_GoodsService(IRepository<Statistical_Goods, Guid> cargoRepository, IRepository<Address, Guid> addrRepository, IRepository<Texture, Guid> textureRepository, IRepository<TmsType, Guid> typeRepository)
        {
            _cargoRepository = cargoRepository;
            _typeRepository = typeRepository;
            _textureRepository = textureRepository;
            _addrRepository = addrRepository;
        }
        #endregion

        #region 显示
        [HttpGet]
        public async Task<PagedResultDto<StatisticalDto>> GetList(int PageIndex, int PageSize, string name, string Typename, string addr, DateTime? star, DateTime? end)
        {
            try
            {
                var a = await _cargoRepository.GetListAsync();
                var b = await _typeRepository.GetListAsync();
                var c = await _textureRepository.GetListAsync();
                var d = await _addrRepository.GetListAsync();

                var linq = (from w in a
                            join x in b on w.TypeID equals x.Id
                            join y in c on w.TextureID equals y.Id
                            join z in d on w.AddrID equals z.Id
                            where (star == null ? true : w.CreateTime >= star)
                           && (end == null ? true : w.CreateTime <= end)
                            select new StatisticalDto
                            {
                                Id = w.Id,
                                StlName = w.StlName,
                                TypeID = x.Id,
                                TextureID = y.Id,
                                SkuName = w.SkuName,
                                AddrID = z.Id,
                                CargoNum = w.CargoNum,
                                GoodsNum = w.GoodsNum,
                                SurplusNum = w.SurplusNum,
                                Space = z.Space,
                                PartentID = z.PartentID,
                                TextureName = y.TextureName,
                                TypeName = x.TypeName,
                                CreateTime = w.CreateTime
                            }).ToList();
                if (!string.IsNullOrEmpty(name))
                {
                    linq = linq.Where(s => s.StlName.Contains(name)).ToList();
                }
                if (!string.IsNullOrEmpty(Typename))
                {
                    linq = linq.Where(s => s.TextureID.Equals(Typename)).ToList();
                }
                if (!string.IsNullOrEmpty(addr))
                {
                    linq = linq.Where(s => s.AddrID.Equals(name)).ToList();
                }

                //总条数
                int TotalCount = linq.Count;

                var result = linq.Skip((PageIndex - 1) * PageSize).Take(PageSize);

                var res = ObjectMapper.Map<List<StatisticalDto>, List<StatisticalDto>>(result.ToList());

                return new PagedResultDto<StatisticalDto>(TotalCount, res);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion
    }
}
